package xyz.scootaloo.oj.offer;

import xyz.scootaloo.console.app.support.FunctionDesc;
import xyz.scootaloo.console.app.support.InvokeProxy;
import xyz.scootaloo.oj.ds.ListNode;
import xyz.scootaloo.oj.ds.helper.CollectionBuilder;

/**
 * 双指针
 * @author flutterdash@qq.com
 * @since 2021/3/28 22:14
 */
public class P52a {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null)
            return null;
        if (headA == headB)
            return headA;

        ListNode pointerA = headA;
        ListNode pointerB = headB;
        byte record = 0;

        while (pointerA != pointerB) {
            boolean currentRecord = false;
            pointerA = pointerA.next;
            pointerB = pointerB.next;
            if (pointerA == null) {
                currentRecord = true;
                pointerA = headB;
            }
            if (pointerB == null) {
                currentRecord = true;
                pointerB = headA;
            }
            if (currentRecord) {
                if (record == 2)
                    return null;
                record++;
            }
        }

        return pointerA;
    }

    public static void main(String[] args) {
        P52a p52A = new P52a();
        System.out.println(testA(InvokeProxy.fun(p52A::getIntersectionNode)));
        System.out.println(testB(InvokeProxy.fun(p52A::getIntersectionNode)));
        System.out.println(testC(InvokeProxy.fun(p52A::getIntersectionNode)));
    }

    protected static boolean testA(FunctionDesc.Rtn2pWrapper<ListNode, ListNode, ListNode> fun) {
        ListNode node_a1 = new ListNode(4);
        ListNode node_a2 = new ListNode(1);

        ListNode node_b1 = new ListNode(5);
        ListNode node_b2 = new ListNode(0);
        ListNode node_b3 = new ListNode(1);
        ListNode node_b4 = new ListNode(8);
        ListNode node_b5 = new ListNode(4);
        ListNode node_b6 = new ListNode(5);

        ListNode headA = CollectionBuilder.getHeadOfList(node_a1, node_a2, node_b4, node_b5, node_b6);
        ListNode headB = CollectionBuilder.getHeadOfList(node_b1, node_b2, node_b3, node_b4, node_b5, node_b6);

        return fun.call(headA, headB) == node_b4;
    }

    protected static boolean testB(FunctionDesc.Rtn2pWrapper<ListNode, ListNode, ListNode> fun) {
        ListNode node_a1 = new ListNode(0);
        ListNode node_a2 = new ListNode(9);
        ListNode node_a3 = new ListNode(1);
        ListNode node_a4 = new ListNode(2);
        ListNode node_a5 = new ListNode(4);

        ListNode node_b1 = new ListNode(3);

        ListNode headA = CollectionBuilder.getHeadOfList(node_a1, node_a2, node_a3, node_a4, node_a5);
        ListNode headB = CollectionBuilder.getHeadOfList(node_b1,  node_a4, node_a5);

        return fun.call(headA, headB) == node_a4;
    }

    protected static boolean testC(FunctionDesc.Rtn2pWrapper<ListNode, ListNode, ListNode> fun) {
        ListNode node_a1 = new ListNode(2);
        ListNode node_a2 = new ListNode(4);
        ListNode node_a3 = new ListNode(6);

        ListNode node_b1 = new ListNode(1);
        ListNode node_b2 = new ListNode(5);

        ListNode headA = CollectionBuilder.getHeadOfList(node_a1, node_a2, node_a3);
        ListNode headB = CollectionBuilder.getHeadOfList(node_b1, node_b2);

        return fun.call(headA, headB) == null;
    }

}
